( function ($) { 
    $(document).ready(function(){

        function api( data, rfn ) {
            $.ajax({
                url: '/api/',
                type: 'POST',
                data: data,
                success: function(rdata){
                    if ((rdata.substr(0,1) == '{') || (rdata.substr(0,1) == '[')) rdata = JSON.parse(rdata);
                    if (rfn) {
                        rfn(rdata);
                    }
                },
                error: function(xhr, ajaxOptions, thrownError){
                    if (xhr.status == 0) { rfn({result:'timeout'}); }
                }
            });
        }

        
        /* Notifications */

        function notify( message, classes, msec ) {
            var el = $('<div class="'+classes+'">'+message+'</div>');
            $('.info-box').prepend(el);
            if ( msec )
            setTimeout(function(){
                $(el).fadeOut(msec, function(){
                    $(el).remove();
                });
            }, msec);
        }


        /* Translations */

        function __( str, context = 'default' ) {
            if (!soc) return str;
            if (!soc.locale) return str;
            if (!soc.translate[soc.locale][context][str]) return str;
            return soc.translate[soc.locale][context][str];
        }


        /* if owner */

        if ($.cookie('soc_login')) {

            /* Highlight avatar */
            $('header .avatar img').attr('style','border: 2px solid aquamarine;');

            /* Delete post */

            $('[data-del]').click(function(){
                var data = {
                    fn:  'del',
                    url: $(this).attr('data-del'),
                }
                if ( confirm(__('Delete?')) ) {
                    api(data, function(r){
                        if ( r.info_text ) {
                            notify( r.info_text, r.info_class, r.info_time );
                        }
                        if ( r.result == 'deleted' ) {
                            $('[data-del="'+data.url+'"]').parents('[data-teaser]').remove();
                        }
                    });
                }
            });
            
            /* Hide post */

            $('[data-hide]').click(function(){
                var data = {
                    fn:  'hide',
                    id:  $(this).attr('data-hide'),
                }
                api(data, function(r){
                    if ( r.info_text ) {
                        notify( r.info_text, r.info_class, r.info_time );
                    }
                    if ( r.result == 'hide' ) {
                        $('[data-hide="'+data.id+'"]').parents('[data-teaser]').remove();
                    }
                });
            });

            /* Edit post */

            $('[data-edit]').click(function(){
                var $page = $(this).attr('data-edit');
                if ( $page ) {
                    window.open( '/edit'+$page, '_blank' );
                } else {
                    window.location = '/edit/';
                }
                return false;
            });

            /* Home */

            $('[data-home]').click(function(){
                $('body,html').animate({ scrollTop: 0 }, 500);
                // and reload page
            });

        }

                    


        /* Login/Install button */

        $('[data-login-button]').click(function(){
            var data = {
                fn: 'login',
                login_url: $('[data-login-form] input[name=login_url]').val(),
                password: $('[data-login-form] input[name=password]').val(),
            }
            api(data, function(r){
                if ( r.info_text ) {
                    notify( r.info_text, r.info_class, r.info_time );
                }
                if (r.result == 'timeout') {
                    notify( "Connection lost", 'info-error', 10000 );
                }
                if (r.result == 'installed') {
                    window.location.reload(true);
                }
                if (r.goto) {
                    window.location.pathname = r.goto;
                }
            });
        });


        /* Cron */

        api({fn:'cron'}, function(r){
            if (r.alert) {
                alert(r.alert);
            }
            if ( r.info_text ) {
                notify( r.info_text, r.info_class, r.info_time );
            }
            if ( r.sitemap == 'updated' ) {

            }
        });

    }); 
})( jQuery );